import { result } from 'lodash';
/*
 * @Description: 
 * @Autor: 商晓彬
 * @Date: 2021-06-24 15:18:24
 * @LastEditors: 商晓彬
 * @LastEditTime: 2022-01-14 11:53:13
 */
import { Component, EventEmitter, Injector, Input, Output, ViewChild } from '@angular/core';
import { AppComponentBase } from '@shared/common/app-component-base';
import { ModalDirective } from 'ngx-bootstrap';
import { CodeFormatDescServiceProxy, CodeFormatDto, CodeFormatRuleDto } from '@shared/service-proxies/service-proxies';
import { finalize } from 'rxjs/operators';
import { MessageService } from '@abp/message/message.service';
@Component({
    selector: 'codingEditOrAdd',
    templateUrl: './coding-edit-or-add.component.html',
    styleUrls: ['./coding-edit-or-add.component.less']
})
export class CodingEditOrAddComponent extends AppComponentBase {
    //引入子组件
    @ViewChild('createOrEditModal') modal: ModalDirective;
    @Output() modalSave: EventEmitter<any> = new EventEmitter<any>();
    @Input() editData: any;  //行数据
    @Input() title: string;  //标题
    active = false;
    form = {
        model: undefined,
        isUsed: true,
        useWhere: undefined,
        stageSign: undefined
    };
    useWhereOptions = [
        {
            label: 'Bom变更',
            value: 'BOM变更送审'
        }, {
            label: '工艺规程变更',
            value: '工艺规程变更'
        },{
            label: 'Bom审核',
            value: '设计BOM送审'
        },{
            label: '异常管理',
            value: '异常管理'
        }, {
            label: '生产准备计划',
            value: '生产准备计划'
        }, {
            label: '出库管理',
            value: '出库管理'
        }, {
            label: '退库管理',
            value: '退库管理'
        }, {
            label: '入库管理',
            value: '入库管理'
        }, {
            label: '火箭生产计划',
            value: '生产计划'
        }
    ]
    coding = [];
    codeOption = [
        {
            label: '常量',
            value: 0,
            disabled: false
        }, {
            label: '日期',
            value: 1,
            disabled: false
        }, {
            label: '流水号',
            value: 2,
            disabled: false
        }
    ];
    dateOption = [
        {
            label: '请选择',
            value: ''
        },
        {
            label: 'yyyyMMdd',
            value: 'yyyyMMdd'
        }, {
            label: 'yyMMdd',
            value: 'yyMMdd'
        }, {
            label: 'yyyyMM',
            value: 'yyyyMM'
        }, {
            label: 'yyMM',
            value: 'yyMM'
        }, {
            label: 'MMdd',
            value: 'MMdd'
        }, {
            label: 'yy',
            value: 'yy'
        }, {
            label: 'MM',
            value: 'MM'
        }
    ];
    saveFlag = true;
    constructor(
        injector: Injector,
        private _codeFormatDescServiceProxy: CodeFormatDescServiceProxy,
        private _messageService: MessageService,
    ) {
        super(injector);
    }

    ngOnInit() {
    }
    //打开弹窗
    onShown(): void {
        if (this.title == '修改编码规则') {
            // this.coding = this.editData.codeFormatRuleDto;
            let index = this.editData.codeFormatRuleDto;
            for (let i = index.length; i > 0; i--) {
                this.coding.push(index[i - 1]);
            }
            this.form.model = this.editData.codeName;
            if (this.editData.isUsed == 1) {
                this.form.isUsed = true;
            } else {
                this.form.isUsed = false;
            }
            this.form.useWhere = this.editData.availableModules;
            this.form.stageSign = this.editData.remark;
            let key = [];
            for (const item of this.coding) {
                key.push(item.ruleKey);
            }
            key.includes(1) ? this.codeOption[1].disabled = true : this.codeOption[1].disabled = false;
            key.includes(2) ? this.codeOption[2].disabled = true : this.codeOption[2].disabled = false;
        }
    }
    //查看新增任务
    show() {
        if (this.title == '新增编码规则') {
            this.close();
        }
        this.modal.show();
        this.active = true;
    }
    //关闭事件
    close(): void {
        this.saveFlag = true;
        this.codeOption[1].disabled = false;
        this.codeOption[2].disabled = false;
        this.coding = [];
        this.form.isUsed = true;
        this.form.model = undefined;
        this.form.stageSign = undefined;
        this.form.useWhere = undefined;
        this.active = false;
        this.modal.hide();
    }
    save() {
        let key = [];
        this.coding.forEach(item => {
            key.push(item.ruleKey);
        });
        if (!key.includes(1) || !key.includes(2)) {
            this._messageService.error('必须包含流水号和日期！');
            return;
        }
        //获取实体
        let input = new CodeFormatDto();
        input.codeName = this.form.model;
        input.availableModules = this.form.useWhere;
        if (this.title == '修改编码规则') {
            input.id = this.editData.id;
        }
        if (this.form.isUsed) {
            input.isUsed = 1;
        } else {
            input.isUsed = 0;
        }
        input.remark = this.form.stageSign;
        input.codeFormatRuleDto = [];
        for (let i = 0; i < this.coding.length; i++) {
            let codeFormatRuleDto = new CodeFormatRuleDto();
            codeFormatRuleDto.ruleKey = this.coding[i].ruleKey;
            codeFormatRuleDto.ruleValue = this.coding[i].ruleValue;
            codeFormatRuleDto.ruleSort = i;
            if (this.coding[i].ruleKey == 2) {
                codeFormatRuleDto.ruleValue = '为了后台校验随便传个';
                codeFormatRuleDto.serialLength = this.coding[i].serialLength;
                codeFormatRuleDto.step = this.coding[i].step;
                codeFormatRuleDto.startNum = this.coding[i].startNum;
            }
            input.codeFormatRuleDto.push(codeFormatRuleDto);
        }
        // for (const item of this.coding) {
        //     let codeFormatRuleDto = new CodeFormatRuleDto();
        //     codeFormatRuleDto.ruleKey = item.ruleKey;
        //     codeFormatRuleDto.ruleValue = item.ruleValue;
        //     if (item.ruleKey == 2) {
        //         codeFormatRuleDto.ruleValue = '为了后台校验随便传个';
        //         codeFormatRuleDto.serialLength = item.serialLength;
        //         codeFormatRuleDto.step = item.step;
        //         codeFormatRuleDto.startNum = item.startNum;
        //     }
        //     input.codeFormatRuleDto.push(codeFormatRuleDto);
        // }
        //将form表单赋值给input的bopdesc下
        if (this.saveFlag) {
            this.saveFlag = false;
            this._codeFormatDescServiceProxy.createOrUpdateCodeFormat(input)
                .pipe(finalize(() => { }))
                .subscribe((result) => {
                    this.saveFlag = true;
                    if (result.code == 0) {
                        this._messageService.success('保存成功');
                        this.close();
                        this.modalSave.emit(null);
                    } else {
                        this._messageService.error(result.message)
                    }

                });
        }

    }
    // 新增编码
    addCoding() {
        let item = { ruleKey: 0 };
        this.coding.push(item);
    }
    // 删除编码
    delCoding(index) {
        if (this.coding[index].ruleKey == 1) {
            this.codeOption[1].disabled = false;
        }
        if (this.coding[index].ruleKey == 2) {
            this.codeOption[2].disabled = false;
        }
        this.coding.splice(index, 1);
    }
    selectChange(event) {
        let key = [];
        this.coding.forEach(item => {
            key.push(item.ruleKey);
        });
        key.includes(1) ? this.codeOption[1].disabled = true : this.codeOption[1].disabled = false;
        key.includes(2) ? this.codeOption[2].disabled = true : this.codeOption[2].disabled = false;
    }
}
